package pl.streamsoft.test.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DatabaseStore {
	private String databaseUrl = "jdbc:postgresql://192.168.72.121/staging";
	private String databaseUser = "postgres";
	private String databasePassword = "postgres";

	private Connection connection;

	private int connectionsCount = 0;

	public String getUser() {
		return databaseUser;
	}

	public String getPassword() {
		return databasePassword;
	}

	public String getURL() {
		return databaseUrl;
	}

	public DatabaseStore() {

	}

	public DatabaseStore(Connection connection) {
		this.connection = connection;
	}

	public void connect() throws SQLException {
		if (isConnected()) {
			disconnect();
		}
		Properties connectionProperties = new Properties();
		connectionProperties.setProperty("user", getUser());
		connectionProperties.setProperty("password", getPassword());

		connection = DriverManager
				.getConnection(getURL(), connectionProperties);
		connectionsCount++;
	}

	public void connect(String user, String password, String url)
			throws SQLException {
		if (isConnected()) {
			disconnect();
		}
		databaseUrl = url;
		databaseUser = user;
		databasePassword = password;

		Properties connectionProperties = new Properties();
		connectionProperties.setProperty("user", user);
		connectionProperties.setProperty("password", password);

		connection = DriverManager.getConnection(url, connectionProperties);
		connectionsCount++;

	}

	public int getConnectionsCount() {
		return connectionsCount;
	}

	public void disconnect() throws SQLException {
		connection.close();
	}

	public Connection getConnection() throws SQLException {
		if (!isConnected()) {
			connect();
		}
		return connection;
	}

	public Statement getStatement() throws SQLException {
		return getConnection().createStatement();
	}

	public Statement getStatementSoftLocked() throws SQLException {
		return getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_UPDATABLE);
	}

	public Statement getStatementHardLocked() throws SQLException {
		return getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);
	}

	public PreparedStatement getPreparedStatement(String query)
			throws SQLException {
		return getConnection().prepareStatement(query);
	}

	public PreparedStatement getPreparedStatementSoftLocked(String query)
			throws SQLException {
		return getConnection().prepareStatement(query,
				ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
	}

	public PreparedStatement getPreparedStatementHardLocked(String query)
			throws SQLException {
		return getConnection().prepareStatement(query,
				ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
	}

	public int getRowsCount(String tableName) throws SQLException {

		Statement statement = getStatement();

		ResultSet resultSet = statement.executeQuery("SELECT count(*) FROM "
				+ tableName);
		if (resultSet.next()) {
			return resultSet.getInt(1);
		}
		return 0;
	}

	public boolean isConnected() throws SQLException {
		return connection != null && !connection.isClosed();
	}

}
